package com.uniledger.exchange.push;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.uniledger.commonlib.util.ToastUtils;
import com.uniledger.exchange.R;
import com.uniledger.exchange.event.MessageEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * @file FileName
 * Uni-Exchange
 *  * liqy
 * Copyright 星期三 YourCompany.
 */
public class PushReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {

        //TODO 数据解析
        String notificationTitle = context.getResources().getString(R.string.app_name);
        String notificationText = "Test notification";
        String notificationAmount = "Test notification";
        String notificationToken = "Test notification";
        // Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
        if (intent.getStringExtra("message") != null) {
            notificationText = intent.getStringExtra("message");
        }

        if (intent.getStringExtra("amount") != null) {
            notificationAmount = intent.getStringExtra("amount");
        }

        if (intent.getStringExtra("token") != null) {
            notificationToken = intent.getStringExtra("token");
        }

        notificationText = String.format(context.getResources().getString(R.string.notification_desc), notificationAmount, notificationToken);

        //TODO 判断通知的形式，根据应用当前的状态
//        if (UniApp.getCurrentAppState() == AppLifeCycleCallback.STATE_NORMAL) {//应用内消息
//            showFloat(notificationText);
//        } else if (UniApp.getCurrentAppState() == AppLifeCycleCallback.STATE_BACK_TO_FRONT) {//应用内消息
//            showFloat(notificationText);
//        } else if (UniApp.getCurrentAppState() == AppLifeCycleCallback.STATE_FRONT_TO_BACK) {//通知栏消息
//            MockPush push = MockPush.buildTransferPush("1", notificationTitle, notificationText);
//            showNotification(context, push);
//        } else {//通知栏消息
//            MockPush push = MockPush.buildTransferPush("1", notificationTitle, notificationText);
//            showNotification(context, push);
//        }

        showFloat(notificationText);
        MockPush push = MockPush.buildTransferPush("1", notificationTitle, notificationText);
        showNotification(context, push);

        //当前页面 扫码页面(自己已收到) 转账页面(对方已收到)

    }

    /**
     * 解析推送数据
     *
     * @param intent
     */
    private void parsePushData(Intent intent) {

    }

    /**
     * 浮动窗口展示
     */
    private void showFloat(String text) {
//        ToastUtils.show(text);
        //发送通知
        EventBus.getDefault().post(new MessageEvent(text));
    }

    /**
     * 当前页面展示
     */
    private void showCurrent(String text) {
        ToastUtils.show("Current:" + text);
    }

    /**
     * 通知栏展示
     */
    private void showNotification(Context context, MockPush mockPush) {
        NotifyUtil.sendMessagingStyleNotification(context, mockPush);
    }
}
